package org.ezplatform.office.meeting.setting.category.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.ezplatform.cloud.common.log.LogService;
import org.ezplatform.component.check.annotation.DataCheck;
import org.ezplatform.core.common.WebUser;
import org.ezplatform.core.pagination.PropertyFilter;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.core.web.controller.BaseController;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.office.meeting.setting.category.entity.BoardroomCategorySet;
import org.ezplatform.office.meeting.setting.category.service.BoardroomCategorySetService;
import org.ezplatform.util.StringUtils;
import org.ezplatform.util.ValidateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: BoardroomCategorySetController
 * @Description: 会议室类型控制类
 */
@RestController
@RequestMapping(value = "/api/w/boardroomCategorySetController")
public class BoardroomCategorySetWebApiController extends BaseController<BoardroomCategorySet, String> {

    private String msg = "";
    
	@Autowired
	private BoardroomCategorySetService service;

	@Autowired
	private LogService logService;
	
	/*
	 * (非 JAVADOC) <p>Title: getEntityService</p> <p>Description: </p>
	 * 
	 * @return
	 * 
	 */
	@Override
	protected BaseService<BoardroomCategorySet, String> getEntityService() {
		return service;
	}

	/**
	 * 
	 * @Title: prepareModel
	 * @Description: 初始化数据
	 * @param request
	 * @param model
	 * @param id
	 * @return void
	 * @throws
	 */
	@ModelAttribute
	public void prepareModel(HttpServletRequest request, Model model,
			@RequestParam(value = "id", required = false) String id) {
		super.initPrepareModel(request, model, id);
	}

	/**
     * 
     * @Title: findByPage
     * @Description: 获取列表数据（翻页）
     * @param request
     * @return
     * @return PageOaMeetingBaseSet
     * @throws
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @RequiresPermissions({"meeting:roomcate:03"})
    public Page findByPage(HttpServletRequest request) {
        WebUser user = WebUtils.getCurrentUser();
        String corpId = user.getCorpId();
        Pageable localPageable = PropertyFilter.buildPageableFromHttpRequest(request);
        //名称
        String name = ValidateUtils.filter(request.getParameter("name"));
        return this.service.getCategorySetList(localPageable,corpId,name,user);
    }
    
    /**
     * 保存/修改
     *
     * @param entity
     * @param model
     * @return
     */
    @RequestMapping(value = "/editSave", method = RequestMethod.POST)
    //@DataCheck(paramKey="id", extraCondi = {"is_init=0"},dataTable="oa_meeting_boardroom_cate",dataField="id",permissionCode="meetingLog:setting:02")
    public OperationResult editSave(@ModelAttribute("entity") BoardroomCategorySet entity, Model model) {
        String id = StringUtils.null2String(entity.getId());
        String oleName = "";
        if(StringUtils.isNotEmpty(id)) {
        	BoardroomCategorySet bcs = this.service.findOne(id);
        	oleName = bcs.getName();
        }
        String name = entity.getName();
        WebUser user = WebUtils.getCurrentUser();
        String corpId = user.getCorpId();
        //保存前 验证
        // 验证名称
        msg = this.service.checkCategorySet(id, name, 1, corpId);
        if (StringUtils.isNotEmpty(msg)) {
            return OperationResult.buildFailureResult(msg);
        }
        //记录日志
  		if(StringUtils.isNotEmpty(id) && logService.checkOptLog(corpId, "meetingLog:setting:02", user.getJwtToken()))
  			this.logService.saveModuleLog("meetingLog:setting:02", oleName, "");  
  		else if(logService.checkOptLog(corpId, "meetingLog:setting:01", user.getJwtToken())) 
  			this.logService.saveModuleLog("meetingLog:setting:01", name, "");
        return super.editSave(entity);
    }
    
    /**
     * 编辑页面
     * 
     * @param model
     * @return
     */
    @RequestMapping(value = "/editShow", method = RequestMethod.GET)
    public OperationResult editShow(HttpServletRequest request) {
        String id = ValidateUtils.filter(request.getParameter("id"));
        BoardroomCategorySet boardroomCategorySet = this.service.findOne(id);
        return OperationResult.buildSuccessResult(boardroomCategorySet);
    }
    
    /**
     * 查看页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/viewShow", method = RequestMethod.GET)
    public Map<String, Object> viewShow(HttpServletRequest request) {
        String id = ValidateUtils.filter(request.getParameter("id"));
        Map<String, Object> result = new HashMap<>();
        
        BoardroomCategorySet boardroomCategorySet = this.service.findOne(id);
        result.put("entity", boardroomCategorySet);
        WebUser user = WebUtils.getCurrentUser();
        if(logService.checkOptLog(user.getCorpId(), "meetingLog:setting:03", user.getJwtToken())) {
	        //记录日志
	        this.logService.saveModuleLog("meetingLog:setting:03", boardroomCategorySet.getName(), "");
        }
        return result;
    }
    
    /**
     * 删除（批量）数据
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @RequiresPermissions({"meeting:roomcate:04"})
    //@DataCheck(paramKey="id", extraCondi = {"is_init=0"},dataTable="oa_meeting_boardroom_cate",dataField="id",permissionCode="meetingLog:setting:04")
    public OperationResult delete(HttpServletRequest request, @RequestParam("ids") String... ids) {
    	WebUser user = WebUtils.getCurrentUser();
    	boolean canLog = logService.checkOptLog(user.getCorpId(), "meetingLog:setting:04", user.getJwtToken());
    	List<BoardroomCategorySet> list = null;
    	if(canLog) list = this.service.findAll(ids);
    	//判断会议类型是否已被使用
    	String msg = this.service.checkDeleteData(ids);
    	if(StringUtils.isNotEmpty(msg)){
    		return OperationResult.buildFailureResult(msg);
    	}
    	//记录日志
    	if(canLog && CollectionUtils.isNotEmpty(list)) {
    		List<Map<String, String>> operateDataList = new ArrayList<Map<String, String>>();
    		for(BoardroomCategorySet bcs : list) {
    			Map<String, String> operateDataMap = new HashMap<>();
				// 操作数据
				operateDataMap.put("operateData", bcs.getName());
				// 操作明细
				operateDataMap.put("operateDetails", "");
				// 请求Id
				operateDataMap.put("requestId", null);
				operateDataList.add(operateDataMap);
    		}
    		// 记录批量日志
			logService.batchSaveModuleLog("meetingLog:setting:04", operateDataList, request);
    	}
        return super.delete(ids);
    }
    
    /**
     * 查询会议室类型树数据
     * 
     * @return
     */
    @RequestMapping(value = "/getCategoryTreeData", method = RequestMethod.GET)
    public OperationResult getCategoryTreeData(HttpServletRequest request) {
        WebUser user = WebUtils.getCurrentUser();
        String corpId = user.getCorpId();
        String jeson = this.service.getCategoryTreeData(request,corpId);
        return OperationResult.buildSuccessResult("", jeson);
    }
    
    @RequestMapping(value = "/getCategoryDate", method = RequestMethod.GET)
    public OperationResult getCategoryDate(HttpServletRequest request) {
    	WebUser user = WebUtils.getCurrentUser();
    	List<Map> list = this.service.getHaveRoomCategorySetList(user.getCorpId());
    	return OperationResult.buildSuccessResult(list);
    }
}
